package com.google.android.apps.wakemewhere;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;


public class AlarmContentObserver extends ContentObserver {
	private final ContentResolver contentResolver;
	private final AlarmArbiter alarmArbiter;

	public AlarmContentObserver(Context context, AlarmArbiter alarmArbiter) {
      super(null);
      this.contentResolver = context.getContentResolver();
      this.alarmArbiter = alarmArbiter;
	}

	@Override
	public void onChange(boolean selfChange) {
		super.onChange(selfChange);
		alarmArbiter.setAlarms(getAlarmsFromResolver());
	}
	
	public List<Alarm> getAlarmsFromResolver() {
		List<Alarm> alarms = new ArrayList<Alarm>();
		Cursor cursor = contentResolver.query(Alarms.CONTENT_URI, null, null, null, null);
		if (!cursor.moveToFirst()) {
			return Collections.emptyList(); 
		}
		do {
			Alarm alarm = new Alarm(cursor);
			alarms.add(alarm);
		} while (cursor.moveToNext());
		return alarms;
	}

	@Override
	public boolean deliverSelfNotifications() {
        return false;
	}
	
}
